// 输入防抖

const input1 = document.getElementById("input1");

let timer = null;

input1.addEventListener("keyup", function () {
  if (timer) {
    clearTimeout(timer);
  }

  timer = setTimeout(() => {
    console.log(input1.value); // 模拟触发change 事件

    // 清空定时器
    timer = null;
  }, 1000);
});

function debounce(fn, delay = 500) {
  let timer = null;
  return function () {
    if (timer) {
      clearTimeout(timer);
    }

    timer = setTimeout(() => {
      fn.apply(this, arguments);
      timer = null;
    }, delay);
  };
}

input1.addEventListener(
  "keyup",
  debounce(() => {
    console.log(input1.value); // 模拟触发change 事件
  }, 600)
);
